import 'package:aio/aio.dart';

class AcombinedUtils {
  ///加法
  static double add(double? d1, String? d2) {
    if (StringUtils.isNotEmpty(d1) && StringUtils.isNotEmpty(d2)) {
      try {
        return DecimalUtils.add(d1, double.parse(d2!))!; //fixme 防止进度丢失。
        //return d1! + double.parse(d2!);
      } catch (e) {}
    } else if (d1 != null) {
      return d1;
    }
    return 0;
  }

  ///乘法
  static double multi(double? d, String? d2) {
    var mul = d;
    if (StringUtils.isNotEmpty(d) && StringUtils.isNotEmpty(d2)) {
      mul = (d! * double.parse(d2!));
    }
    if (mul != null) {
      return mul;
    }
    return 0;
  }

  ///合计数量
  static String getNum(double d) {
    return d.toInt().toString();
  }

  static int fractionDigits = 2;

  ///合计面积
  ///d 单个面积
  ///num 数量
  static String getArea(double d, {String? num}) {
    var str = d.toString();
    if (StringUtils.isNotEmpty(num)) {
      str = (d * int.parse(num!)).toString();
    }
    return StringUtils.toStringAsFixedWithString(str, fractionDigits);
  }

  ///合计面积 保留四位小数
  static String getArea4(double d, {String? num}) {
    var str = d.toString();
    if (StringUtils.isNotEmpty(num)) {
      str = (d * int.parse(num!)).toString();
    }
    return StringUtils.toStringAsFixedWithString(str, 4);
  }

  ///合计重量 保留三位小数
  static String getWeight3(double d, {String? num}) {
    var str = d.toString();
    if (StringUtils.isNotEmpty(num)) {
      str = (d * int.parse(num!)).toString();
    }
    return StringUtils.toStringAsFixedWithString(str, 3);
  }

  ///合计周长
  ///d 单个面积
  ///num 数量
  static String getPerimeter(double d, {String? num}) {
    var str = d.toString();
    if (StringUtils.isNotEmpty(num)) {
      str = (d * int.parse(num!)).toString();
    }
    return StringUtils.toStringAsFixedWithString(str, fractionDigits);
  }

  ///合计金额
  ///d 金额
  ///num 数量
  static String getMoney(double d, {String? num}) {
    var str = d.toString();
    if (StringUtils.isNotEmpty(num)) {
      str = (d * int.parse(num!)).toString();
    }
    return StringUtils.toStringAsFixedWithString(str, fractionDigits);
  }
}
